﻿using DAL3;
using S7.Net;
using System;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using static GEMINI_OP20.FormMain;

namespace GEMINI_OP20
{
    public partial class FormUserLoginInterface : Form
    {
        public FormUserLoginInterface()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.richTextBoxUser.Text.Trim().Length == 0)
            {
                MessageBox.Show("用户名不能为空！", "用户提示");
                this.richTextBoxUser.Focus();
                return;
            }
            if (this.textBox1.Text.Trim().Length == 0)
            {
                MessageBox.Show("密码不能为空！", "用户提示");
                this.textBox1.Focus();
                return;
            }
            Program.userlogin = UserManagement.GetUser(richTextBoxUser.Text, textBox1.Text);
            if (Program.userlogin.Admin == null)
            {
                MessageBox.Show("用户名或密码错误！", "用户提示");
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                
            }

            this.Close();
        }

        private void FormUserLoginInterface_Load(object sender, EventArgs e)
        {
            richTextBoxUser.Text = "1";
            textBox1.Text = "1";
            //取消跨线程的访问
            Control.CheckForIllegalCrossThreadCalls = false;
            //#region 设置串口参数并打开串口接收
            ////波特率

            ////IC卡
            //serialPort1.BaudRate = 9600;
            
            ////数据位

            ////IC卡
            //serialPort1.DataBits = 8;
            
            ////奇偶校验位
            //serialPort1.Parity = System.IO.Ports.Parity.None;
            
            ////停止位
            //serialPort1.StopBits = System.IO.Ports.StopBits.One;
            //string[] contents = new string[3];
            //try
            //{
            //     contents = File.ReadAllLines(Application.StartupPath + "\\COM口配置.txt", Encoding.UTF8);
            //}
            //catch
            //{
            //    MessageBox.Show("请检查COM口配置！"); 
            //}
            ////端口
            //try
            //{
            //    serialPort1.PortName = contents[0];
            //    serialPort1.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
            //    serialPort1.Open();

            //}
            //catch
            //{
            //    MessageBox.Show("IC卡端口打开失败，请检查串口连接是否正确！", "错误提示");
            //}
            //#endregion
            
        }
        //IC卡
        string icID;
        private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {

                Thread.Sleep(50);
                byte[] byteRead1 = new byte[serialPort1.BytesToRead];
                serialPort1.Read(byteRead1, 0, byteRead1.Length);
                byte[] temp6 = new byte[1];
                temp6[0] = byteRead1[20];
                icID = byteToHexStr(temp6, 1);
                Program.userlogin = UserManagement.GetUser(Convert.ToInt32(icID));
                this.DialogResult = DialogResult.OK;
                
                
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc + "接收失败");
            }

        }
        public string byteToHexStr(byte[] bytes, int len)  //数组转十六进制字符
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < len; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }
    }
}
